
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;

Page({
  data: {
    my_uid: false,
    aaaaaa: '',
    bbbbbb: '',
    help_replace_money_points:'',
    deductionContentData:[],
    box_bg_true: false,
    deductionContent_info: null,
    deductionContent_setting: null,
    deductionContent_task: null,
    mydikouMoney:0,
    deductionContent_total: null,
    jindu:'',
    endtimes: '',
    timer: null,
    timer1:null,
    zhanshi_ind:0,
    share_bimg:{}
  },
  zhanshi_tab_ind() {
    let n = this.data.zhanshi_ind;
    // console.log(n, this.data.yaoqing_zhanshi_data.length, this.data.zhanshi_ind,88);
    if (n < this.data.deductionContent_total.list.length - 1) {
      n++;
    } else {
      n = 0;
    }
    this.setData({
      zhanshi_ind: n
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  box_bg_hide(){
    this.setData({
      box_bg_true: false
    })
  },
  box_bg_show() {
    this.setData({
      box_bg_true: true
    })
  },
  // go_order_detail(){
  //   wx.navigateTo({
  //     url: '/pages/order_detail/order_detail'
  //   })
  // },
  add0fn(n){
    return n<10?'0'+n:''+n;
  },
  deductionContent() {
    let that = this;
    let k = wx.getStorageSync('key');
    //index.php?act=other_member&op=get_replace_money_info
    wx.request({
      url: ApiUrl + 'index.php?act=other_member&op=get_replace_money_info',
      data: {
        key:k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('抵扣金界面信息:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            console.log(result.data.datas);
            let res = result.data.datas;

            let times = res.setting.surplus_time;
            if (res.setting.state == 1 && times>0){
              that.getDuration(times);
              clearInterval(that.data.timer);
              that.data.timer = setInterval(function () {

                if (that.data.time == 1) {
                  clearInterval(that.data.timer);
                }
                times--;
                that.getDuration(times);
                // console.log(111);
              }, 1000)
              

            }


            let str = (parseFloat(res.task.money) - parseFloat(res.task.vmoney)) / parseFloat(res.task.money) * 100 + '%';
            // let str = (parseFloat(res.task.money) - 10) / parseFloat(res.task.money) * 100 + '%';

            let mydikouMoney = (res.task.money - res.task.vmoney).toFixed(2);

            
            console.log(res.total,'修改====》');
            for (let i = 0; i < res.total.list.length;i++){

              res.total.list[i].member_name = that.parseStringToStar(res.total.list[i].member_name);

              // console.log((typeof res.total.list[i].create_time));
              if ((typeof res.total.list[i].create_time) == 'string') {
                res.total.list[i].create_time = res.total.list[i].create_time.substring(res.total.list[i].create_time.length - 2);
              }
          
            }

            that.setData({
              deductionContent_info: res.info,
              deductionContent_setting: res.setting,
              deductionContent_task: res.task,
              deductionContent_total: res.total,
              jindu:str,
              mydikouMoney: mydikouMoney
            })

          }
        }
      }
    });
  },
  parseStringToStar(str) {
    return str.length > 2 ? str.substr(0, 1) + new Array(str.length - 2).join('*') + str.substr(-1) : str
  },
  getDuration(second) {
    let that = this;
    var days = Math.floor(second / 86400);
    var hours = Math.floor((second % 86400) / 3600);
    var minutes = Math.floor(((second % 86400) % 3600) / 60);
    var seconds = Math.floor(((second % 86400) % 3600) % 60);
    // var duration = days + "天" + that.add0fn(hours) + "小时" + that.add0fn(minutes) + "分" + that.add0fn(seconds) + "秒";
    var duration =  that.add0fn(hours) + "小时" + that.add0fn(minutes) + "分" + that.add0fn(seconds) + "秒";

    that.setData({
      endtimes: duration
    })
  },
  //index.php?act=other_member&op=help_replace_money
  //赚钱-抵扣金助力
  help_replace_money(){
    let that = this;
    let k = wx.getStorageSync('key');
    let fid = wx.getStorageSync('fid');

    wx.request({
      url: ApiUrl + 'index.php?act=other_member&op=help_replace_money',
      data: {
        act:'other_member',
        op:'help_replace_money',
        key: k,
        uid: fid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('抵扣金助力:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            console.log(result.data.datas);
            let res = result.data.datas;
            that.setData({
              help_replace_money_points: res,
              my_uid: false,
              aaaaaa: k,
              bbbbbb: fid
            })

            that.deductionContent();
          }
        }
      }
    });
  },
  onLoad: function (option) {
    let that = this;
    if (option.uid){//邀请信息
      console.log(option);
      wx.setStorageSync('fid', option.uid);
      that.setData({
        my_uid: true
      })
    }


    this.share_bimg();
    this.deductionContent();
  },
    /**
     * 生命周期函数--监听页面卸载
     */
  onUnload: function () {
    // wx.reLaunch({
    //   url: '../make_money/make_money'
    // })
  },
  //index.php?act=other&op=share_bimg
  //背景图设置
  share_bimg() {

    let that = this;
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=share_bimg',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        // console.log('背景图设置:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas;
            // console.log('背景图设置:==>', res);
            that.setData({
              share_bimg: res
            })
          }
        }
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let uid = wx.getStorageSync('userId');
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '邀请好友，助力拿现金',
      imageUrl: '/image/WechatIMG91.jpeg',//图片地址
      path: '/pages/get_money/get_money?uid=' + uid+'&&type=replace_money',// 用户点击首先进入的当前页面
      success: function (res) {
        // 转发成功
        console.log("转发成功:");
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:");
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.deductionContent();
    let that = this;
    that.data.timer1 = setInterval(() => {
      that.zhanshi_tab_ind();
    }, 3000)
    console.log('监听页面显示');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let that = this;
    clearInterval(that.data.timer1);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})
